﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common;
using Microsoft.SharePoint.Client;
using System.Threading;

namespace Navigation.Views
{
    public partial class NavigationView : UserControl
    {
        
        private ClientContext oContext;
        private String SharepointUser;
        
        public NavigationView()
        {
            InitializeComponent();
            
        }

        private void GetCurrentUser()
        {
            
            oContext = new ClientContext("http://dmxsp01/EHS");
            
            try
            {
                //MessageBox.Show("Beginning Server Query now...");
                oContext.Load(oContext.Web, s => s.CurrentUser);
                oContext.ExecuteQueryAsync((s, e) =>
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                            {
                                string[] names = oContext.Web.CurrentUser.Title.Split(' ');
                                GlobalVars.UserFirstName = names[0];
                                GlobalVars.UserLastName = names.Last();
                                GlobalVars.UserTitle = oContext.Web.CurrentUser.Title;
                                GlobalVars.UserEmail = oContext.Web.CurrentUser.Email;
                                GlobalVars.UserLogin = oContext.Web.CurrentUser.LoginName;
                                ribbonMain.Title2 = GlobalVars.UserTitle + " - ";
                            });
                        
                    },
                    (s, e) =>
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() => {
                            MessageBox.Show("Request Failed, " + e.Exception.ToString());
                            
                        });
                        
                    });
            }
            catch (Exception err)
            {
                MessageBox.Show("Sychronous error occurred:" + err.ToString());
            }
        }
        
        private void Ribbon_Loaded(object sender, RoutedEventArgs e)
        {
            
            GetCurrentUser();
            
        }
    }
}
